package com.example.rpc_easy.Serializable;

import java.io.*;

/**
 * JDK 序列化器
 */
public class JdkSerializable implements Serializable1 {
    /**
     * 序列化
     * @param object
     * @return
     * @param <T>
     * @throws IOException
     */
        @Override
        public <T> byte[] serialize(T object) throws IOException {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(object);
            objectOutputStream.close();
            return outputStream.toByteArray();
        }

    /**
     * 反序列化
     * @param bytes
     * @param type
     * @return
     * @param <T>
     * @throws IOException
     */
        @Override
        public <T> T deserialize(byte[] bytes, Class<T> type) throws IOException {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            try {
                return (T) objectInputStream.readObject();
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } finally {
                objectInputStream.close();
            }
        }
    }


